package whackEm.persistent;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Canvas;
import android.graphics.Paint;

public  class GameState {

	private final String TAG = GameState.class.getSimpleName();
	private Context context;
	private int rightMolesHitTOTAL = 0;
	private int wrongMolesHitTOTAL = 0;
	public int rightMolesCounter = 0;
	public int wrongMolesCounter = 0;
	public  int level = 0;
	public  long timer = 0;
	private int moleToHit = 0;
	private int moleHitCounter = 1;
	public int howManyTimesYouPlayed =0;
	//Game Mode
	private  int gameMode = 0;
	public  int lives;
	public  int maxLives = 3;
	public  int timeToIncrease = 1000;
	public  int timeToDecrease = 5000;
	public int redClockLevel  = 1;
	public int blueClockLevel = 1;
   	
	
	public final static int MODE_FREEPLAY = 0;
	public final static int MODE_TIMED = 1;	
	
	private SharedPreferences sp;
	private Editor editor;
	public final static String PREFSNAME = "WHACKEM_PREFS";
	public final static String MAX_LIVES = "MAX_LIVES";
	public final static String RED_CLOCK_LEVEL = "RED_CLOCK_LEVEL";
	public final static String BLUE_CLOCK_LEVEL = "RED_CLOCK_LEVEL"; // how many moles you were supposed to hit in the level
	public final static String LEVEL = "LEVEL";	
	public final static String TIMER = "TIMER";
	private static final String HOW_MANY_TIMES_YOU_PLAYED = "HOW_MANY+_TIMES";
	private Paint paint = new Paint(Paint.FAKE_BOLD_TEXT_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
	
	public GameState(Context c) {		
		this.context = c;
		paint.setTextSize(20);		
		paint.setARGB(200, 0, 80, 150);	
		loadSharedPreferences();
		lives = maxLives;		
		setGameTimer();
		howManyTimesYouPlayed++;
	}
	public void update(int level, long timer){				
	}

	

   public void setGameTimer(){ 
		timer = (level > 50) ? 
				60 : 15+level ;	
   }

/**
 * @return the moleToHit
 */
public  int getMoleToHit() {
	return moleToHit;
}

public  void setMoleToHit(int animal){
    moleToHit = animal; 
}

	public void draw(Canvas canvas) {	
		
		canvas.drawText("Points:" + rightMolesCounter, 0,30, paint);		
		canvas.drawText("Level:" +level,0,70,paint);
		canvas.drawText("Lives:" + lives,0,110,paint);
	}
	/**
	 * @return the moleHitCounter
	 */
	public  int getMoleHitCounter() {
		return moleHitCounter;
	}
	
	public  void addMoleHitCounter(int i) {
		moleHitCounter+=i;		
		if (moleHitCounter <=0)
			moleHitCounter=0;
			
	}
	
	/**
	 * @param moleHitCounter the moleHitCounter to set
	 */
	public void setMoleHitCounter(int moleHitCounter) {
		this.moleHitCounter = moleHitCounter;
	}
	/**
	 * @return the gameMode
	 */
	public  int getGameMode() {
		return gameMode;
	}
	/**
	 * @param gameMode the gameMode to set
	 */
	public  void setGameMode(int gameMode) {
		this.gameMode = gameMode;
	}
	
	public void loadSharedPreferences(){ 
			   sp = context.getSharedPreferences(PREFSNAME,Context.MODE_PRIVATE);
			   maxLives = sp.getInt(MAX_LIVES, 3);			   
			   redClockLevel = sp.getInt(RED_CLOCK_LEVEL, 1);
			   blueClockLevel = sp.getInt(BLUE_CLOCK_LEVEL, 1);
			   level = sp.getInt(LEVEL, 1);
			   howManyTimesYouPlayed = sp.getInt(HOW_MANY_TIMES_YOU_PLAYED,1);
			   
		}		
	
	public void saveSharedpreferences() { 
		editor = sp.edit();
		editor.putInt(MAX_LIVES, maxLives);		
		editor.putInt(RED_CLOCK_LEVEL,redClockLevel);
		editor.putInt(BLUE_CLOCK_LEVEL,blueClockLevel);
		editor.putInt(LEVEL,level);
		editor.putInt(HOW_MANY_TIMES_YOU_PLAYED,howManyTimesYouPlayed);
		editor.commit();
	}
	
	public boolean isPlayingForTheFirstTime(){
		if (howManyTimesYouPlayed == 1)
			return true;
		return false;
	}
	
}

